/**
 * Created by wujo5 on 1/5/2018.
 */
const config = require('../config/expressCongfig');
const mongoose = require('mongoose'), url = config.mongodb.uri;
var dbPath = "mongodb://" + (config.mongodb.options.user ? config.mongodb.options.user + ":" + config.mongodb.options.pass + "@" : "") +
                   url;
var options = {
    server: {
        auto_reconnect: true,
        poolSize: 10
    }
};
/**
 * 连接
 */
mongoose.Promise = global.Promise;
mongoose.connect(dbPath,options, function onMongooseError(err){
	if (err) {
    console.log("Attempting to connect to MongoDB " + dbPath);
		console.log("Mongoose error " + err);
		throw err;
	}
	console.log('Connected to MongoDB at ' + dbPath);
}); 


/**
 * 连接成功
 */
mongoose.connection.on('connected', function () {
  console.log('Mongoose connection :' + url);
});

/**
 * 连接异常
 */
mongoose.connection.on('error',function (err) {
  console.log('Mongoose connection error : ' + err);
});

/**
 * 连接断开
 */
mongoose.connection.on('disconnected', function () {
  console.log('Mongoose disconnected');
});

module.exports = mongoose;
